package com.kitty.game.fuling.service;

import com.kitty.common.db.CacheServiceLJ;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fuling.Fusheninfo;
import com.kitty.game.fuling.RespReqFuling;
import com.kitty.game.fuling.model.Fuling;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.utils.dirtywords.WordService;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FulingService {


  private static final String LOG_DATE_FORMAT = "yyyy-MM-dd HH:mm";

  @Autowired
  Dao dao;

  @Autowired
  RoleService roleService;

  @Autowired
  WordService wordService;

  @Autowired
  CacheServiceLJ cacheServiceLJ;

  public Fuling getFulingdata(int role_id) {
    EquipService bean = SpringUtils.getBean(EquipService.class);
    Fuling fuling = (Fuling)this.dao.fetch(Fuling.class, (Condition)Cnd.where("role_id", "=", Integer.valueOf(role_id)));
    if (fuling != null){
      fuling.doAfterInit();

      Role joinRole = SpringUtils.getRoleService().getOnlinePlayer(role_id);
      int count = bean.getCount("铸灵石", true, joinRole);
      fuling.setZhulingshi(count);
    }else{
      Role role = SpringUtils.getRoleService().getOnlinePlayer(role_id);
      fuling = new Fuling();
      fuling.setJingyan(Integer.valueOf(0));
      fuling.setJieshu(Integer.valueOf(0));
      fuling.setJishu(Integer.valueOf(0));
      int count1 = bean.getCount("铸灵石", true, role);
      fuling.setZhulingshi(Integer.valueOf(count1));
      fuling.setBaihu(Integer.valueOf(1));
      fuling.setQinglong(Integer.valueOf(1));
      fuling.setZhuque(Integer.valueOf(1));
      fuling.setXuanwu(Integer.valueOf(1));
      fuling.setRole_id(role.getRoleId());
      fuling.setType1((byte)0);
      fuling.setType2((byte)0);
      fuling.setType3((byte)0);
      fuling.setType4((byte)0);
      fuling.setWawa_id("");
      fuling.setChongwu_id("");
      fuling.setFushen1_id("");
      fuling.setFushen2_id("");
      fuling.setFushen3_id("");
      int jingyan = fuling.getJingyan().intValue();
      int jieshu = fuling.getJieshu().intValue();
      int jishu = fuling.getJishu().intValue();
      RespReqFuling respReqFuling = new RespReqFuling();
      respReqFuling.setJingyan(jingyan ^ (17 + jieshu) * 16 * 16 + 17 + jishu);
      respReqFuling.setJieshu((byte)(jieshu ^ 0x22));
      respReqFuling.setJishu((byte)(jishu ^ jieshu));
      respReqFuling.setZhulingshi((byte)1);
      respReqFuling.setQinglong((byte)1);
      respReqFuling.setBaihu((byte)1);
      respReqFuling.setZhuque((byte)1);
      respReqFuling.setXuanwu((byte)1);
      List<Fusheninfo> fushenList = new ArrayList<>();
      respReqFuling.setList(fushenList);
      respReqFuling.setaByte((byte)0);
      MessagePusher.pushMessage(role, (Message)respReqFuling);
    }
    return fuling;
  }
}
